#ifndef XGE_UNITTEST_XLFS_H_
#define XGE_UNITTEST_XLFS_H_
#include "xge_common_config.h"
#include "xunit_test.h"
#include "xfsm.h"
#include "xfs.h"
#include "xfile.h"
#include "xdir.h"
#include "xarchive.h"
#include "xassert.h"
#include "xfile_general_operation.h"

using xge::FileSystemManager;
using xge::FileSystemPtr;
using xge::FilePtr;
using xge::ArchivePtr;
using xge::Dir;

#include <windows.h>


#if 0
// Testing case 1 
XUNIT_TEST(XLocalFS,Case1) {
  // Load XFileSystem
  XFileSystemPtr local_fs = XFileSystemManager::GetFileSystem( kLocalFileSystemName );
  X_ASSERT( !local_fs.Null() );
  // Can not begin 
  X_ASSERT( local_fs->Begin() == NULL );
  // testing folder existed
  // Directory : 
  // C:\\MyFolder

  xge::CreateFolder(_xT("C:\\MyFolder"));

  X_ASSERT( 
    local_fs->IsArchiveExisted(_xT("C:\\MyFolder"))&&
    local_fs->IsExisted( _xT("C:\\MyFolder")) &&
    !local_fs->IsFileExisted(_xT("C:\\MyFolder"))
    );

  // create a file 

  ::HANDLE file_handle = ::CreateFileW(
    _xT("C:\\MyFolder\\JustTest.txt"),
    GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_READ|FILE_SHARE_WRITE,
    NULL,
    CREATE_ALWAYS,
    FILE_ATTRIBUTE_NORMAL,
    NULL
    );
  

  char * content = "hello lfs!";
  DWORD dwWriteByte;
  X_ASSERT( ::WriteFile( 
    file_handle ,
    content,
    strlen(content),
    &dwWriteByte,
    NULL
    ));
  ::CloseHandle(file_handle);

  X_ASSERT(file_handle != INVALID_HANDLE_VALUE );

  // now lets test this file
  X_ASSERT(
    !local_fs->IsArchiveExisted(_xT("C:\\MyFolder\\JustTest.txt")) && 
    local_fs->IsFileExisted(_xT("C:\\MyFolder\\JustTest.txt")) &&
    local_fs->IsExisted(_xT("C:\\MyFolder\\JustTest.txt"))
    );
  // load the archive file here

  XFilePtr target_file = local_fs->LoadFile( _xT("C:\\MyFolder\\JustTest.txt") );
  // load two same handle to the file 
  // check if we can have many read-only file at the same time
  XFilePtr tpf = local_fs->LoadFile( _xT("C:\\MyFolder\\JustTest.txt") );
  X_ASSERT( !tpf.Null() );


  X_ASSERT( !target_file.Null() );
  X_ASSERT_BINARY(NEQ,INT,target_file->Length(),0);
  void* dynamic_buffer = malloc( sizeof(char)* target_file->Length() );
  size_t loadSize = target_file->Load(dynamic_buffer,target_file->Length());
  X_ASSERT_BINARY(EQ,INT,target_file->Length(),loadSize);
  free(dynamic_buffer);
}

#else
#include <stdio.h>
// Testing so XArchive function 
UNIT_TEST(XLocalFS,Case2) {

  FileSystemPtr local_fs = FileSystemManager::GetFileSystem( kLocalFileSystemName );
  XASSERT( !local_fs.Null() );

  // Get a archive to iterating 
  // D:\lib temp is only existed on MY COMPUTER 
  ArchivePtr archive_ptr = local_fs->LoadArchive( _xT("D:\\lib temp") );
  XASSERT( !archive_ptr.Null() );
  //iterating the archive
  DirPtr dir = archive_ptr->Begin();
  XASSERT( dir != NULL );
  while( dir->HasNext() ) {
    if( dir->Type() == Dir::TYPE_FILE ) {
      wprintf(_xT("File: %s\n"),dir->name().c_str());
      XASSERT( !(dir->File()).Null() );
      XASSERT( (dir->Archive()).Null() );
    } if( dir->Type() == Dir::TYPE_ARCHIVE ) {
      wprintf(_xT("Dic: %s\n"),dir->name().c_str());
      XASSERT( !(dir->Archive()).Null() );
      XASSERT( (dir->File()).Null() );
    }
    dir->Next();
  }
  archive_ptr.Reset();
  archive_ptr = local_fs->LoadArchive(_xT("C:\\MyFolder"));
  XASSERT( !archive_ptr.Null() );
  // load the file in this folder
  FilePtr file_ptr = archive_ptr->LoadFile(_xT("JustTest.txt"));
  XASSERT( !file_ptr.Null()  );
  // printf hello lfs :)
  char* buffer = (char*)malloc( sizeof(char)* file_ptr->Length() +1 );
  buffer[file_ptr->Length()] = '\0';
  file_ptr->Load( buffer , file_ptr->Length() );
  printf("haha: %s\n", buffer );
  
  free(buffer);
  ::getchar();

}
#endif



#endif// XGE_UNITTEST_XLFS_H_